/*
 * Heap.hpp
 *
 *  Created on: 21/09/2011
 *      Author: marcio
 */

#ifndef HEAP_HPP_
#define HEAP_HPP_

#include "heap.h"

class Heap {
private:
	void * const	b;                  // base array
	size_t 			nmemb;              // number of elements
	const size_t	asize;				// array size (in elements)
	const size_t 	msize;  			// member size (in bytes)
public:
	Heap(void * const array, const size_t asize, const size_t msize);
	bool build(const size_t nmemb);
	bool up(int i);
	bool offer(void * x);
	bool poll();
	virtual int compar(const void *, const void *) = 0;
};

inline Heap::Heap(void * const array, const size_t asize, const size_t msize) : b(array), nmemb(0), asize(asize), msize(msize) {
}

inline bool Heap::build(const size_t nmemb) {
	if (nmemb <= asize)
		return false;
	this->nmemb = nmemb;
	heapify(b, nmemb, msize, compar);
	return true;
}



inline bool up(int i, ){
	if(i > nmemb) return false;
	heapup(vb, i, msize, compar);
	return true;
}

inline bool offer(void * x){
	if(msize <= nmemb) return false;
	heapoffer(void *x, void *vb, nmemb, msize, compar);
	return true;
}

inline bool poll(){
	if(msize <= 0) return false;
	heappoll(vb, nmemb, msize, compar);
	return true;
}


#endif /* HEAP_HPP_ */
